package com.ermace.robo.jumper.game;

import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.ermace.robo.jumper.MainActivity;
import com.ermace.robo.jumper.R;
import com.ermace.robo.jumper.WinScreen;

public class Game extends Activity {
	
	private GameView gameView;
	private SharedPreferences highScores;
	private SharedPreferences parameters;
	private Map currentMap;
	private Intent intent;
	
	private Button resumeButton;
	private Button retryButton;
	private Button quitButton;
	
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // call super's onCreate method
        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
//        final ProgressDialog dialog = ProgressDialog.show(Game.this, "", "Loading. Please wait...", true);
        
        setContentView(R.layout.loadingscreen);
		
//        ImageView img = (ImageView) findViewById(R.id.loadingImg);
//        img.startAnimation(AnimationUtils.loadAnimation(this, R.anim.loading_rot));
        
        intent = new Intent(this, WinScreen.class);
		currentMap = getIntent().getExtras().getParcelable("currentMap");
        
		highScores =  getSharedPreferences("highScores", MODE_PRIVATE);
		parameters =  getSharedPreferences("parameters", MODE_PRIVATE);
		
//    	currentMap = MapList.getMapByName(parameters.getString("currentMap", null));
        
	    LoadingContent loading = new LoadingContent();
		loading.execute();
        
//		dialog.dismiss();
		
    }
	
	private void doneLoading() {
		setContentView(R.layout.game); // inflate the layout
		
        gameView = (GameView)findViewById(R.id.gameView);
        gameView.initParameters(parameters);
        gameView.initCurrentMap(currentMap);
        gameView.initIntent(intent);
        
        // allow volume keys to set game volume
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
    }
	
	@Override
	public void onBackPressed() {
		if (gameView != null && !gameView.isPaused())
			PauseGame();
	};
	 // when the app is pushed to the background, pause it
    @Override
    public void onPause() {
    	super.onPause();	// call the super method
    	if (gameView != null && !gameView.isPaused())
    		PauseGame();
    	MainActivity.pauseMusic();
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	
		MainActivity.resumeMusic();
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    this.finish();
    }
    
    // release resources
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	gameView.stopThreads();
    }
	
	// called when the user touches the screen in this Activity
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	if (gameView != null)
    		return gameView.handleEvent(event);
    	
    	return true;
    }
    
    private class LoadingContent extends AsyncTask<URL, Integer, Long> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
	    @Override
		protected Long doInBackground(URL... urls) {
	    	currentMap.load(getResources());
	    	
	    	return null;
	    }

	    @Override
		protected void onProgressUpdate(Integer... progress) {
	    	
	    }

	    @Override
		protected void onPostExecute(Long result) {
	    	doneLoading();
	    }
	}
    
    public void PauseGame()
    {

    	
    	gameView.pause();
    	
    	setContentView(R.layout.pause_screen);
    	
    	resumeButton = (Button) findViewById(R.id.resumeButton);
		retryButton = (Button) findViewById(R.id.retryButton);
		quitButton = (Button) findViewById(R.id.quitButton);
		
		resumeButton.setOnTouchListener(resumeButtonListener);
		retryButton.setOnTouchListener(retryButtonListener);
		quitButton.setOnTouchListener(quitButtonListener);
    	
    }
    
    private OnTouchListener resumeButtonListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			
			resumeButton.setBackgroundResource(R.drawable.choosemap_button_pressed);
			
			switch(event.getAction()) {
				case MotionEvent.ACTION_OUTSIDE:
					resumeButton.setBackgroundResource(R.drawable.choosemap_button);
					break;
					
				case MotionEvent.ACTION_UP:
					resumeButton.setBackgroundResource(R.drawable.choosemap_button);
					MainActivity.startingNewActivity = true;
					if (gameView != null) {
						setContentView(gameView);
						gameView.resume();
					}
					
					return true;
			}
			
			return true;
		}
	};
	
	
    private OnTouchListener retryButtonListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			
			retryButton.setBackgroundResource(R.drawable.choosemap_button_pressed);
			
			switch(event.getAction()) {
				case MotionEvent.ACTION_OUTSIDE:
					retryButton.setBackgroundResource(R.drawable.choosemap_button);
					break;
					
				case MotionEvent.ACTION_UP:
					retryButton.setBackgroundResource(R.drawable.choosemap_button);
					if (gameView != null) {
						setContentView(gameView);
						gameView.newGame();
					}
					return true;
			}
			
			return true;
		}
	};
	
    private OnTouchListener quitButtonListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			
			quitButton.setBackgroundResource(R.drawable.choosemap_button_pressed);
			
			switch(event.getAction()) {
				case MotionEvent.ACTION_OUTSIDE:
					quitButton.setBackgroundResource(R.drawable.choosemap_button);
					break;
					
				case MotionEvent.ACTION_UP:
					quitButton.setBackgroundResource(R.drawable.choosemap_button);
					MainActivity.startingNewActivity = true;
					onDestroy();
					finish();
					return true;
			}
			
			return true;
		}
	};
}
